///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_PYTHON_AUTO_FUNCTIONS_H__
#define __BUOLA_PYTHON_AUTO_FUNCTIONS_H__

namespace buola { namespace python {

void python_init();
void python_finish();

void run_code(const std::string &pCode,const std::string &pFilename=std::string());
void run_file(const io::CURI &pURL);
void run_interactive();

bool fetch_error(YObject &pVar,YObject &pValue,YObject &pTraceback,bool pNormalize=true);

YModule get_imported_module(const std::string &pName);
YModule get_main_module();
YModule get_builtins_module();
YModule import_module(const std::string &pName);
YModule import_source_module(const io::CURI &pURL,const std::string &pName=std::string());

void add_to_search_path(const std::string &pPath);
void remove_from_search_path(const std::string &pPath);

inline bool error_occurred()
{
    return PyErr_Occurred();
}

inline void error_print()
{
    PyErr_Print();
}

inline void error_clear()
{
    PyErr_Clear();
}

namespace detail {
    template<typename tArg>
    struct SArgParser
    {
        SArgParser(tArg &pObj)
            :   mObj(pObj)
        {}
        
        ~SArgParser()
        {
            if(!mObj.IsNull())
            {
                mObj.IncRef();
            }
        }
        
        operator PyObject**()   {   return mObj.GetPointerToPyObject();    }
        
        tArg &mObj;
    };
    
/*namespace detail*/ }

template<typename... tArgs>
bool parse_args(const YObject &pSrc,const std::string &pStr,tArgs&... pArgs)
{
    return PyArg_ParseTuple(pSrc,pStr.c_str(),(PyObject**)detail::SArgParser<tArgs>(pArgs)...);
}

std::string make_python_identifier(const std::string &pS);
std::wstring make_python_identifier(const std::wstring &pS);

/*namespace python*/ } /*namespace buola*/ }

#endif
